from flask import Flask, send_from_directory, current_app
import config
from exts import db
from flask_migrate import Migrate
from logHandler import getLogHandler
import ssl
# 导入bp蓝图
from blueprints.swiper import bp as swiper_bp
from blueprints.blog import bp as blog_bp
from blueprints.user import bp as user_bp
from blueprints.images import bp as image_bp
from blueprints.event import bp as event_bp
from blueprints.place import bp as place_bp
from blueprints.comment import bp as comment_bp
from blueprints.starlist import bp as starlist_bp
from blueprints.baike import bp as baike_bp



# 在项目必经之处导入Model文件，让项目知道model的存在
from models.user import UserModel
from models.basicInfo import BasciInfoModel
from models.blog import BlogModel
from models.comment import CommentModel
from models.event import EventModel
from models.interestingplace import InterestingPlaceModel
from models.starlist import StarListModel
from models.img import ImageModel


app = Flask(__name__)
# 绑定配置文件
app.config.from_object(config.DevelopmentConfig)
# 对db对象完成初始化
db.init_app(app)
# 创建迁移对象
migrate = Migrate(app, db)
# 添加日志配置
app.logger.addHandler(getLogHandler())
# # 配置ssl
# context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
# context.load_cert_chain('cert.crt', 'cert.key')

# 注册蓝图
app.register_blueprint(blog_bp)  # 注册博客蓝图
app.register_blueprint(swiper_bp)  # 注册swiper蓝图
app.register_blueprint(user_bp)  # 注册用户蓝图
app.register_blueprint(image_bp)  # 注册图片蓝图
app.register_blueprint(comment_bp)  # 注册评论蓝图
app.register_blueprint(starlist_bp)  # 注册收藏蓝图
app.register_blueprint(baike_bp)  # 注册百科蓝图
app.register_blueprint(event_bp)  # 注册活动蓝图
app.register_blueprint(place_bp)  # 注册景点蓝图


@app.route('/')
def hello_world():
    image_dir = current_app.config['IMAGE_ROOT']
    return send_from_directory(image_dir, '小程序码.png', mimetype='image/png')


@app.route('/favicon.ico')
def favicon():
    image_dir = current_app.config['IMAGE_ROOT']
    return send_from_directory(image_dir, 'favicon.ico', mimetype='image/vnd.microsoft.icon')


if __name__ == '__main__':
    app.run()
